﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebComponents.Data;
using WebComponents.Security;
using WebComponents;
using System.IO;
using GBIT.Web;

public partial class Banner : UserControlBase {
  private int advID = 0;
  protected void Page_Load(object sender, EventArgs e) {
    this.Title = "Quảng cáo";
    this.TaskTitle = "Thêm mới";
    advID = ToInt(Request.Params["id"]);
    if (!Page.IsPostBack) {
      AdvertiseInfo ads = AdvertiseController.GetByAdsId(ConvertType.ToInt(Request.Params["id"]));
      if (ads != null) {
        this.TaskTitle = "Cập nhật";
        txtWidth.Text = ads.Width.ToString();
        txtHeight.Text = ads.Height.ToString();
        txtUrl.Text = ads.NavigateUrl;
        txtTitle.Text = ads.AdvTitle;
        ImagePath1.ImagePath = ads.MediaUrl;
        ImagePath1.ImageUrl = ads.MediaUrl;
        chkActive.Checked = (ads.Status == 1) ? true : false;
        if (ads.Status == 1)
          chkActive.Checked = true;
        else
          chkActive.Checked = false;
        cmbPosition.SelectedIndex = -1;
        if (cmbPosition.Items.FindByValue(ads.Position) != null)
          cmbPosition.Items.FindByValue(ads.Position).Selected = true;
      }      
    }
    this.SaveEnabled = true;
  }
  protected override void OnSaveRecord(bool redirect) {
    AdvertiseInfo adv = null;
    if (advID > 0)
      adv = AdvertiseController.GetByAdsId(advID);
    if (adv == null)
      adv = new AdvertiseInfo();
    adv.AdvTitle = txtTitle.Text;
    adv.AdvType = cmbLoaiQC.SelectedValue;
    adv.CultureID = cmbLanguage.SelectedValue;
    adv.Height = ToInt(txtHeight.Text);
    adv.Width = ToInt(txtWidth.Text);
    adv.Target = cmbTarget.SelectedValue;
    adv.NavigateUrl = txtUrl.Text;
    adv.MediaUrl = ImagePath1.ImagePath;
    adv.Status = (chkActive.Checked) ? 1 : 0;
    adv.Position = cmbPosition.SelectedValue;
    if (advID <= 0) {
      adv.CreatedDate = DateTime.Now;
      AdvertiseController.Insert(adv);
      Security.ShowClientMessage(Page, "Thêm quảng cáo mới thành công", string.Format("?mod={0}", WebComponents.ConvertType.Encode("QuangCao/BannerManager.ascx")));
    } else {
      AdvertiseController.Update(adv);
      Security.ShowClientMessage(Page, "Cập nhật quảng cáo thành công", string.Format("?mod={0}", WebComponents.ConvertType.Encode("QuangCao/BannerManager.ascx")));
    }
  }
  protected void cmbLanguage_SelectedIndexChanged(object sender, EventArgs e) {

  }
}
